package com.wbdgj.ui.branch;

import android.content.Context;
import android.content.Intent;
import android.text.InputType;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.internal.LinkedTreeMap;
import com.wbdgj.R;
import com.wbdgj.base.BaseActivity;
import com.wbdgj.base.BaseApplication;
import com.wbdgj.http.HttpAdapter;
import com.wbdgj.http.OnResponseListener;
import com.wbdgj.model.ResultObjectModel;
import com.wbdgj.utils.SpKeyUtils;
import com.wbdgj.utils.ToastUtils;
import com.wbdgj.utils.dialog.ConfirmDialog;

import java.text.DecimalFormat;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import in.srain.cube.views.ptr.PtrClassicFrameLayout;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrHandler;
import retrofit2.Call;

public class NetfeeRoamingActivity extends BaseActivity implements PtrHandler {

    private Context context = this;

    @BindView(R.id.pfl_root)
    PtrClassicFrameLayout pfl_root;

    @BindView(R.id.srje)
    EditText srje;

    @BindView(R.id.branchName)
    TextView branchName;
    @BindView(R.id.outBranchName)
    TextView outBranchName;
    @BindView(R.id.wfyeTxt)
    TextView wfyeTxt;
    @BindView(R.id.bjyeTxt)
    TextView bjyeTxt;
    @BindView(R.id.jlyeTxt)
    TextView jlyeTxt;

    private String wfye;
    private String bjye;

    private String outBranchIdTxt, outBranchNameTxt;
    private String inBranchIdTxt, inBranchNameTxt;
    private String union_type;

    @Override
    protected int getLayoutResId() {
        return R.layout.ui_netfree_roaming;
    }

    @Override
    protected void initView() {
        srje.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
        outBranchIdTxt = getIntent().getStringExtra("id");
        outBranchNameTxt = getIntent().getStringExtra("name");
        branchName.setText(outBranchNameTxt);
        memberinfo();
        pfl_root.setEnabledNextPtrAtOnce(true);
        pfl_root.setLastUpdateTimeRelateObject(this);
        pfl_root.setPtrHandler(this);
        pfl_root.setKeepHeaderWhenRefresh(true);
//        rocordgroupb();
//        upbranchlist();
    }

    @OnClick({R.id.zcsm, R.id.qrzcBtn, R.id.text_top_right, R.id.allMoney, R.id.outBranchLay, R.id.inBranchLay})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.zcsm:
                if(TextUtils.isEmpty(union_type)){
                    final ConfirmDialog confirmDialog = new ConfirmDialog(context, "暂无说明！" , 8);
                    confirmDialog.show();
                    confirmDialog.setCanceledOnTouchOutside(false);
                    confirmDialog.setClicklistener(new ConfirmDialog.ClickListenerInterface() {
                        @Override
                        public void doConfirm() {
                            confirmDialog.dismiss();
                        }

                        @Override
                        public void doCancel() {
                            confirmDialog.dismiss();
                        }
                    });
                }else{
                    if (union_type.equals("9")){
                        final ConfirmDialog confirmDialog = new ConfirmDialog(context, "当前转出门店未加入漫游联盟，暂不能进行漫游！" , 8);
                        confirmDialog.show();
                        confirmDialog.setCanceledOnTouchOutside(false);
                        confirmDialog.setClicklistener(new ConfirmDialog.ClickListenerInterface() {
                            @Override
                            public void doConfirm() {
                                confirmDialog.dismiss();
                            }

                            @Override
                            public void doCancel() {
                                confirmDialog.dismiss();
                            }
                        });
                        ToastUtils.toastShort("");
                        return;
                    }else if (union_type.equals("1")){
                        final ConfirmDialog confirmDialog = new ConfirmDialog(context, "您好，您现在处于余额漫游联盟，您可以把所有余额漫游至您想要去的店，点确定后开始您的愉快之旅吧！" , 8);
                        confirmDialog.show();
                        confirmDialog.setCanceledOnTouchOutside(false);
                        confirmDialog.setClicklistener(new ConfirmDialog.ClickListenerInterface() {
                            @Override
                            public void doConfirm() {
                                confirmDialog.dismiss();
                            }

                            @Override
                            public void doCancel() {
                                confirmDialog.dismiss();
                            }
                        });
                        return;
                    }else if (union_type.equals("0")){
                        final ConfirmDialog confirmDialog = new ConfirmDialog(context, "您好，您现在处于本金漫游联盟，您只可以漫游您的本金，不可漫游赠送的网费，点确定后开始您的愉快之旅吧！" , 8);
                        confirmDialog.show();
                        confirmDialog.setCanceledOnTouchOutside(false);
                        confirmDialog.setClicklistener(new ConfirmDialog.ClickListenerInterface() {
                            @Override
                            public void doConfirm() {
                                confirmDialog.dismiss();
                            }

                            @Override
                            public void doCancel() {
                                confirmDialog.dismiss();
                            }
                        });
                    }
                }

                break;
            case R.id.outBranchLay:
                startActivityForResult(new Intent(context, ChoiceBranchOutActivity.class).putExtra("id", outBranchIdTxt), 1);
                break;
            case R.id.inBranchLay:
                startActivityForResult(new Intent(context, ChoiceBranchActivity.class).putExtra("id", outBranchIdTxt), 2);
                break;
            case R.id.allMoney:
                srje.setText(wfye);
                break;
            case R.id.text_top_right:
                launch(NetfeeRoamingHistoryActivity.class);
                break;
            case R.id.qrzcBtn:
                if(TextUtils.isEmpty(outBranchNameTxt)){
                    ToastUtils.toastShort("请选择转出门店！");
                    return;
                }
                if(TextUtils.isEmpty(inBranchIdTxt)){
                    ToastUtils.toastShort("请选择转入门店！");
                    return;
                }
                if (TextUtils.isEmpty(srje.getText().toString())){
                    ToastUtils.toastShort("请输入转出金额！");
                    return;
                }


//                if (union_type.equals("9")){
//                    ToastUtils.toastShort("当前门店未加入漫游联盟，暂不能进行漫游！");
//                    return;
//                }else if (union_type.equals("1")){
//                    if (moneyBig(Double.parseDouble(wfye), Double.parseDouble(srje.getText().toString()))){
//                        transfer();
//                    }else{
//                        ToastUtils.toastShort("漫游金额不能大于会员余额！");
//                        return;
//                    }
//                }else if (union_type.equals("0")){
//                    if (moneyBig(Double.parseDouble(bjye), Double.parseDouble(srje.getText().toString()))){
//                        transfer();
//                    }else{
//                        ToastUtils.toastShort("漫游金额不能大于本金余额！");
//                        return;
//                    }
//                }
                transfer();
//                if (union_type.equals("1")){
//                    if (moneyBig(Double.parseDouble(wfye), Double.parseDouble(srje.getText().toString()))){
//                        transfer();
//                    }else{
//                        ToastUtils.toastShort("漫游金额不能大于会员余额！");
//                        return;
//                    }
//                }else if (union_type.equals("0")){
//                    if (moneyBig(Double.parseDouble(bjye), Double.parseDouble(srje.getText().toString()))){
//                        transfer();
//                    }else{
//                        ToastUtils.toastShort("漫游金额不能大于本金余额！");
//                        return;
//                    }
//                }else{
//                    transfer();
//                }
                break;
        }
    }

    private void transfer() {
        Call<ResultObjectModel> call = HttpAdapter.getSerives().transfer(BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.TOKEN), outBranchIdTxt, inBranchIdTxt, srje.getText().toString());
        call.enqueue(new OnResponseListener<ResultObjectModel>(context) {
            @Override
            protected void onSuccess(ResultObjectModel userModelBaseResultModel) {
                if(userModelBaseResultModel.getResultCode().equals("0000")){
                    LinkedTreeMap linkedTreeMap = (LinkedTreeMap) userModelBaseResultModel.getData();
                    startActivityForResult(new Intent(context, NetfeeRoamingSuccessActivity.class).putExtra("msg", linkedTreeMap.get("msg")+""),3);
                }else{
                    LinkedTreeMap linkedTreeMap = (LinkedTreeMap) userModelBaseResultModel.getData();
                    startActivityForResult(new Intent(context, NetfeeRoamingFaultActivity.class).putExtra("msg", userModelBaseResultModel.getResultMsg()+""),3);
                }
            }
        });
    }

    //网吧会员信息
    private void memberinfo() {
        Call<ResultObjectModel> call = HttpAdapter.getSerives().memberinfo(BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.TOKEN), outBranchIdTxt);
        call.enqueue(new OnResponseListener<ResultObjectModel>(context) {
            @Override
            protected void onSuccess(ResultObjectModel userModelBaseResultModel) {
                if(userModelBaseResultModel.getResultCode().equals("0000")){
                    DecimalFormat decimalFormat = new DecimalFormat("###################.###########");
                    LinkedTreeMap linkedTreeMap = (LinkedTreeMap) userModelBaseResultModel.getData();
                    LinkedTreeMap linkedTreeMapMember = (LinkedTreeMap) linkedTreeMap.get("member");
                    LinkedTreeMap linkedTreeMapBranch = (LinkedTreeMap) linkedTreeMap.get("branch");
                    wfyeTxt.setText(linkedTreeMapMember.get("level")+",会员余额：￥"+linkedTreeMapMember.get("remain")+"");
                    bjyeTxt.setText("本金余额：￥"+linkedTreeMapMember.get("principal")+"");
                    jlyeTxt.setText("奖励余额：￥"+linkedTreeMapMember.get("encourage")+"");
                    wfye = linkedTreeMapMember.get("remain")+"";
                    bjye = linkedTreeMapMember.get("principal")+"";

                    List<LinkedTreeMap> dataLink = (List<LinkedTreeMap>) linkedTreeMapBranch.get("data");
                    if (dataLink.size() == 0){
                        union_type = "9";
//                        final ConfirmDialog confirmDialog = new ConfirmDialog(context, "当前门店未加入漫游联盟，暂不能进行漫游！请选择其它转出门店" , 8);
//                        confirmDialog.show();
//                        confirmDialog.setCanceledOnTouchOutside(false);
//                        confirmDialog.setClicklistener(new ConfirmDialog.ClickListenerInterface() {
//                            @Override
//                            public void doConfirm() {
//                                confirmDialog.dismiss();
//                            }
//
//                            @Override
//                            public void doCancel() {
//                                confirmDialog.dismiss();
//                            }
//                        });
                    }else{
                        union_type = decimalFormat.format(dataLink.get(0).get("union_type"));
//                        if (union_type.equals("9")){
//                            final ConfirmDialog confirmDialog = new ConfirmDialog(context, "当前门店未加入漫游联盟，暂不能进行漫游！请选择其它转出门店" , 8);
//                            confirmDialog.show();
//                            confirmDialog.setCanceledOnTouchOutside(false);
//                            confirmDialog.setClicklistener(new ConfirmDialog.ClickListenerInterface() {
//                                @Override
//                                public void doConfirm() {
//                                    confirmDialog.dismiss();
//                                }
//
//                                @Override
//                                public void doCancel() {
//                                    confirmDialog.dismiss();
//                                }
//                            });
//                        }
                    }
                }
            }
        });
    }

//    private void rocordgroupa() {
//        Call<ResultObjectModel> call = HttpAdapter.getSerives().rocordgroupa(BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.TOKEN), outBranchIdTxt);
//        call.enqueue(new OnResponseListener<ResultObjectModel>(context) {
//            @Override
//            protected void onSuccess(ResultObjectModel userModelBaseResultModel) {
//                if(userModelBaseResultModel.getResultCode().equals("0000")){
//                    DecimalFormat decimalFormat = new DecimalFormat("###################.###########");
//                    LinkedTreeMap linkedTreeMap = (LinkedTreeMap) userModelBaseResultModel.getData();
//                    LinkedTreeMap linkedTreeMapMember = (LinkedTreeMap) linkedTreeMap.get("member");
//                    wfyeTxt.setText("网费余额：￥"+linkedTreeMapMember.get("remain")+"");
//                    wfye = linkedTreeMapMember.get("remain")+"";
//                }
//            }
//        });
//    }


//    private void upbranchlist() {
//        Call<ResultObjectModel> call = HttpAdapter.getSerives().upbranchlist(BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.TOKEN));
//        call.enqueue(new OnResponseListener<ResultObjectModel>(context) {
//            @Override
//            protected void onSuccess(ResultObjectModel userModelBaseResultModel) {
//                if(userModelBaseResultModel.getResultCode().equals("0000")){
//                    DecimalFormat decimalFormat = new DecimalFormat("###################.###########");
//
//                }
//            }
//        });
//    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode){
            case 1://转出门店
                if (resultCode == 3){
                    outBranchIdTxt = data.getExtras().getString("branch_id");
                    outBranchNameTxt = data.getExtras().getString("branch_name");
                    branchName.setText(outBranchNameTxt);
                    Log.e("zp", "out="+outBranchIdTxt+outBranchNameTxt);
                    memberinfo();

                    inBranchIdTxt = "";
                    inBranchNameTxt = "";
                    outBranchName.setText("");
                }
                break;
            case 2://转入门店
                if (resultCode == 4){
                    inBranchIdTxt = data.getExtras().getString("branch_id");
                    inBranchNameTxt = data.getExtras().getString("branch_name");
                    outBranchName.setText(inBranchNameTxt);
                    Log.e("zp", "in="+inBranchIdTxt+inBranchNameTxt);
                }
                break;
            case 3:
                if (resultCode == 1){
                    setResult(1);
                    finish();
                }else if (resultCode == 2){

                }
                break;
        }
    }

    private static boolean moneyBig(Double money01, Double money02){
        if (money01 >= money02){
            return true;
        }else{
            return false;
        }
    }

    @Override
    public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
        return PtrDefaultHandler.checkContentCanBePulledDown(frame, content, header);
    }

    @Override
    public void onRefreshBegin(PtrFrameLayout frame) {
        pfl_root.refreshComplete();
        memberinfo();
    }
}
